import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useAuthStore } from "@/stores/auth";
import { ElMessage } from "element-plus";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "Home",
    component: () => import("@/views/home/index.vue"),
    alias: ["/home"],
    meta: { title: "首页" },
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      title: "用户登录",
      hideNavbar: true, // 登录页面隐藏导航栏
    },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/register/index.vue"),
    meta: {
      title: "用户注册",
      hideNavbar: true, // 注册页面隐藏导航栏
    },
  },
  // 用户管理
  {
    path: "/users",
    name: "Users",
    component: () => import("@/views/users/index.vue"),
    meta: {
      title: "用户管理",
    },
  },
  // 菜单管理
  {
    path: "/menus",
    name: "Menus",
    component: () => import("@/views/menus/index.vue"),
    meta: {
      title: "菜单管理",
    },
  },
  {
    path: "/roles",
    name: "Roles",
    component: () => import("@/views/roles/index.vue"),
    meta: {
      title: "角色管理",
    },
  },
  // 仪表盘
  {
    path: "/dashboard",
    name: "Dashboard",
    component: () => import("@/views/dashboard/index.vue"),
    meta: {
      title: "仪表盘",
    },
  },
  // 商品管理
  {
    path: "/products",
    name: "Products",
    component: () => import("@/views/products/index.vue"),
    meta: {
      title: "商品管理",
    },
  },
  {
    path: "/categories",
    name: "Categories",
    component: () => import("@/views/categories/index.vue"),
    meta: {
      title: "分类管理",
    },
  },
  // 订单管理
  {
    path: "/orders",
    name: "Orders",
    component: () => import("@/views/orders/index.vue"),
    meta: {
      title: "订单管理",
    },
  },
  // 内容管理
  {
    path: "/articles",
    name: "Articles",
    component: () => import("@/views/articles/index.vue"),
    meta: {
      title: "文章管理",
    },
  },
  // 系统管理
  {
    path: "/settings",
    name: "Settings",
    component: () => import("@/views/settings/index.vue"),
    meta: {
      title: "系统设置",
    },
  },
  {
    path: "/logs",
    name: "Logs",
    component: () => import("@/views/logs/index.vue"),
    meta: {
      title: "系统日志",
    },
  },
  // 数据报表
  {
    path: "/reports",
    name: "Reports",
    component: () => import("@/views/reports/index.vue"),
    meta: {
      title: "数据报表",
    },
  },
  // 个人中心
  {
    path: "/profile",
    name: "Profile",
    component: () => import("@/views/profile/index.vue"),
    meta: { title: "个人中心" },
  },
  // 文章列表
  {
    path: "/article-list",
    name: "ArticleList",
    component: () => import("@/views/article-list/index.vue"),
    meta: { title: "文章列表" },
  },
  // 发布文章
  {
    path: "/article-create",
    name: "ArticleCreate",
    component: () => import("@/views/article-create/index.vue"),
    meta: { title: "发布文章" },
  },
  // 产品列表
  {
    path: "/product-list",
    name: "ProductList",
    component: () => import("@/views/product-list/index.vue"),
    meta: { title: "产品列表" },
  },
  // 我的订单
  {
    path: "/my-orders",
    name: "MyOrders",
    component: () => import("@/views/my-orders/index.vue"),
    meta: { title: "我的订单" },
  },
  // 购物车
  {
    path: "/cart",
    name: "Cart",
    component: () => import("@/views/cart/index.vue"),
    meta: { title: "购物车" },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) return savedPosition;
    return { top: 0, behavior: "smooth" };
  },
});

// 路由守卫
router.beforeEach((to, _from, next) => {
  const authStore = useAuthStore();

  // 需要认证的路由
  const requiresAuth = !["Login", "Register"].includes(to.name as string);

  if (requiresAuth && !authStore.isAuthenticated) {
    ElMessage.warning("请先登录");
    // 需要认证但未登录，跳转到登录页
    next({ name: "Login" });
  } else if ((to.name === "Login" || to.name === "Register") && authStore.isAuthenticated) {
    // 已登录用户访问登录/注册页，跳转到首页
    next({ name: "Home" });
  } else {
    // 正常访问
    next();
  }
});

// 根据路由元信息设置页面标题
router.afterEach((to) => {
  const title = (to.meta?.title as string) || "NestJS 学习项目";
  document.title = title;
});

export default router;
